package ru.usu.gv.gui.frames;

import ru.usu.gv.core.ResourceLoader;
import ru.usu.gv.gui.component.GVDropDownButtonPanel;
import ru.usu.gv.gui.component.IDropDownItem;
import ru.usu.gv.gui.frames.styles.GUIStyleUtilities;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.styles.StyleUtilities;
import ru.usu.gv.uicomponents.IUIVertexType;
import ru.usu.gv.uicomponents.UIVertexTypeRegistry;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

/**
 * @author spupyrev
 * 02.11.2008
 * 
 * TODO: union with edge???
 */
public abstract class EditVertexPanelBase extends JPanel
{
	private List<ISaveCallback> saveCallbacks;

	public EditVertexPanelBase(List<ISaveCallback> saveCallbacks, boolean initialize)
	{
		this.saveCallbacks = saveCallbacks;
		if (initialize)
			initializeComponent();
	}

	public EditVertexPanelBase(List<ISaveCallback> saveCallbacks)
	{
		this(saveCallbacks, false);
	}

	protected void initializeComponent()
	{
		setBorder(new TitledBorder("Вершина"));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		add(createColorPanel());
		add(createSizePanel());
		add(createViewPanel());
		add(createLabelFontPanel());
	}

	private JPanel createLabelFontPanel()
	{
		//TODO: implement
		JPanel labelPanel = new JPanel();
		labelPanel.setBorder(new TitledBorder("Метка"));
		//labelPanel.setLayout(new GridLayout(1, 4));

		JButton changeFont = new JButton("Шрифт");
		labelPanel.add(changeFont, BorderLayout.WEST);

		JLabel label = new JLabel();
		label.setIcon(ResourceLoader.loadImage("images/label.png"));
		labelPanel.add(label);
		return labelPanel;
	}

	private JPanel createViewPanel()
	{
		JPanel viewPanel = new JPanel();
		viewPanel.setBorder(new TitledBorder("Форма"));
		viewPanel.setLayout(new GridLayout(1, 4));

		List<IDropDownItem> vTypes = new ArrayList<IDropDownItem>();
		List<String> vertexTypeIds = UIVertexTypeRegistry.getVertexTypeIds();
		final String activeVertexTypeId = getVertexTypeId();
		IDropDownItem activeItem = null;
		for (String id : vertexTypeIds)
		{
			IUIVertexType type = UIVertexTypeRegistry.findVertexType(id);
			IDropDownItem dropDownItem = GUIStyleUtilities.createDropDownItem(type.getIconUrl(), type.getTitle(), type.getId());
			vTypes.add(dropDownItem);
			if (id.equals(activeVertexTypeId))
				activeItem = dropDownItem;
		}

		final GVDropDownButtonPanel dropDownPanel = new GVDropDownButtonPanel(GUIStyleUtilities.createEmptyAction(), vTypes, activeItem);
		viewPanel.add(dropDownPanel);

		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				String newVertexTypeId = dropDownPanel.getActiveItem().getValue();
				if (!activeVertexTypeId.equals(newVertexTypeId))
					setVertexTypeId(newVertexTypeId);
			}

		});
		return viewPanel;
	}

	private JPanel createSizePanel()
	{
		JPanel sizePanel = new JPanel();
		sizePanel.setBorder(new TitledBorder("Размер"));

		final double defaultSize = StyleFacade.getDefaultDoubleAttribute(StyleFacade.VERTEX_SIZE_ATTRIBUTE);
		final double size = getVertexSize();
		final JSlider sizeSlider = new JSlider(1, 100, (int) (50.0 * size / defaultSize));
		sizePanel.add(sizeSlider);

		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				double currentSize = sizeSlider.getValue();
				double newSize = (currentSize * defaultSize) / 50.0;
				if (Math.abs(size - newSize) > 1e-5)
					setVertexSize(newSize);
			}

		});
		return sizePanel;
	}

	private JPanel createColorPanel()
	{
		JPanel colorPanel = new JPanel();
		colorPanel.setBorder(new TitledBorder("Цвет"));
		final JLabel colorLabel = new JLabel();

		JButton chooseColorButton = new JButton("Изменить");
		colorPanel.add(chooseColorButton, BorderLayout.WEST);
		chooseColorButton.addActionListener(GUIStyleUtilities.getChooseColorListener(colorLabel));

		colorLabel.setPreferredSize(new Dimension(80, 25));
		final String sColor = getVertexColor();
		colorLabel.setBackground(StyleUtilities.decodeColor(sColor));
		colorLabel.setOpaque(true);
		colorPanel.add(colorLabel, BorderLayout.EAST);

		saveCallbacks.add(new ISaveCallback()
		{
			public void doSave()
			{
				Color newColor = colorLabel.getBackground();
				String encodedColor = StyleUtilities.encodeColor(newColor);
				if (!sColor.equals(encodedColor))
					setVertexColor(encodedColor);
			}
		});
		return colorPanel;
	}

	protected abstract String getVertexTypeId();

	protected abstract void setVertexTypeId(String newVertexTypeId);

	protected abstract String getVertexColor();

	protected abstract void setVertexColor(String encodedColor);

	protected abstract double getVertexSize();

	protected abstract void setVertexSize(double newSize);

}
